﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;

using FundSms.Client.IDAL;
using FundSms.Client.Model;
using FundSms.DBUtility;

namespace FundSms.Client.OracleDAL
{
    public class SmsSendLog : ISmsSendLog
    {
        /// <summary>
        /// 插入语句
        /// </summary>
        private const string SQL_INSERT_LOG = "INSERT INTO TSMS_SEND_LOG(C_MOBILENO,C_CONTENT,C_PROPERTY,C_FROM,C_SMSNAME,C_SMSID,C_DATE,C_LOGTIME,C_R1,C_R2,C_R3,C_R4,C_R5) values (:C_MOBILENO,:C_CONTENT,:C_PROPERTY,:C_FROM,:C_SMSNAME,:C_SMSID,:C_DATE,sysdate,:C_R1,:C_R2,:C_R3,:C_R4,:C_R5)";
        private const string SQL_INSERT_LOG_BATCH = "INSERT INTO TSMS_SEND_LOG(C_MOBILENO,C_CONTENT,C_PROPERTY,C_FROM,C_SMSNAME,C_SMSID,C_DATE,C_LOGTIME,C_R1,C_R2,C_R3,C_R4,C_R5) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}',sysdate,'{7}','{8}','{9}','{10}','{11}')";

        private const string PARM_C_MOBILENO = ":C_MOBILENO";
        private const string PARM_C_CONTENT = ":C_CONTENT";
        private const string PARM_C_PROPERTY = ":C_PROPERTY";
        private const string PARM_C_FROM = ":C_FROM";
        private const string PARM_C_SMSNAME = ":C_SMSNAME";
        private const string PARM_C_SMSID = ":C_SMSID";
        private const string PARM_C_DATE = ":C_DATE";

        private const string PARM_C_R1 = ":C_R1";
        private const string PARM_C_R2 = ":C_R2";
        private const string PARM_C_R3 = ":C_R3";
        private const string PARM_C_R4 = ":C_R4";
        private const string PARM_C_R5 = ":C_R5";

        #region ISmsSendLog 成员

        public void Insert(TSMSSendLogInfo loginfo)
        {
            OracleParameter[] logparms = GetLogParameters();

            logparms[0].Value = loginfo.CMobileno;
            logparms[1].Value = loginfo.CContent;
            logparms[2].Value = loginfo.CProperty;
            logparms[3].Value = loginfo.CFrom;
            logparms[4].Value = loginfo.CSmsname;
            logparms[5].Value = loginfo.CSmsID;
            logparms[6].Value = loginfo.CDate;
            logparms[7].Value = loginfo.CR1;
            logparms[8].Value = loginfo.CR2;
            logparms[9].Value = loginfo.CR3;
            logparms[10].Value = loginfo.CR4;
            logparms[11].Value = loginfo.CR5;

            OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionStringClientSms, CommandType.Text, SQL_INSERT_LOG, logparms);
        }

        public void InsertBatch(string sql)
        {
            OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionStringClientSms, CommandType.Text, sql, null);
        }

        public void InsertList(List<TSMSSendLogInfo> li)
        {
            
            StringBuilder sb = new StringBuilder();

            sb.Append("BEGIN");

            object[] obj = new object[11];

            int i = 1;

            foreach (TSMSSendLogInfo loginfo in li)
            {
                obj[0] = loginfo.CMobileno;
                obj[1] = loginfo.CContent;
                obj[2] = loginfo.CProperty;
                obj[3] = loginfo.CFrom;
                obj[4] = loginfo.CSmsname;
                obj[5] = loginfo.CSmsID;
                obj[6] = loginfo.CDate;
                obj[7] = loginfo.CR1;
                obj[8] = loginfo.CR2;
                obj[9] = loginfo.CR3;
                obj[10] = loginfo.CR4;
                obj[11] = loginfo.CR5;

                sb.Append(string.Format(SQL_INSERT_LOG_BATCH, obj));
                sb.Append(";");

                i++;
            }

            sb.Append("END");

            OracleHelper.ExecuteNonQuery(OracleHelper.ConnectionStringClientSms, CommandType.Text, sb.ToString(), null);
           
        }

        /// <summary>
        /// 获得缓存的参数
        /// </summary>
        /// <returns></returns>
        private static OracleParameter[] GetLogParameters()
        {
            OracleParameter[] parms = OracleHelper.GetCachedParameters(SQL_INSERT_LOG);

            if (parms == null)
            {
                parms = new OracleParameter[] {												 
					new OracleParameter(PARM_C_MOBILENO, OracleType.VarChar, 1300),
					new OracleParameter(PARM_C_CONTENT, OracleType.VarChar, 140),
					new OracleParameter(PARM_C_PROPERTY, OracleType.VarChar,2),
					new OracleParameter(PARM_C_FROM, OracleType.VarChar, 20),
					new OracleParameter(PARM_C_SMSNAME, OracleType.VarChar, 36),
					new OracleParameter(PARM_C_SMSID, OracleType.VarChar, 50),
					new OracleParameter(PARM_C_DATE, OracleType.VarChar, 50),
					new OracleParameter(PARM_C_R1, OracleType.VarChar, 50),
					new OracleParameter(PARM_C_R2, OracleType.VarChar, 50),
                    new OracleParameter(PARM_C_R3, OracleType.VarChar, 50),
                    new OracleParameter(PARM_C_R4, OracleType.VarChar, 50),
                    new OracleParameter(PARM_C_R5, OracleType.VarChar, 50)};

                OracleHelper.CacheParameters(SQL_INSERT_LOG, parms);
            }

            return parms;
        }

        #endregion
    }
}
